package com.reciver.service.impl;

import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import com.reciver.entity.TbsDbwy;
import com.reciver.entity.TbsDbwy6;
import com.reciver.mapper.TbsDbwy6Mapper;
import com.reciver.mapper.TbsDbwyMapper;
import org.springframework.stereotype.Service;
import com.reciver.common.enums.DataSourceType;
import com.reciver.service.ZiXianBeiDouService;
import com.reciver.common.annotation.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lgb
 * @date 2020-8-24
 * @说明 智想北斗数据获取
 **/
@Service @Slf4j
public class ZiXianBeiDouServiceImpl implements ZiXianBeiDouService{
	
	@Resource
	private TbsDbwyMapper tbsDbwyMapper;
	@Resource
	private TbsDbwy6Mapper tbsDbwy6Mapper;
	@Resource
    private JdbcTemplate jdbcTemplate;
	/**
	 * @param time 时间
	 * @param reads 读取次数
	 * @return 获取北斗sqlservice数据
	 **/
	@Override
	@DataSource(DataSourceType.db2)
	@Transactional(readOnly=true)
	public List<TbsDbwy> findDbwyList(String time,int reads,Set<String> devIdList) {
		List<TbsDbwy> dbwyList=new ArrayList<>();
		List<String> ids = new ArrayList<String>();
		for(String id : devIdList) {
			ids.add(id);
		}
		List<TbsDbwy> idList=tbsDbwyMapper.findTbsDbwyList(time,reads,ids);
		log.info("读取数据为:"+idList);
		if(idList==null || idList.isEmpty()) {
			return dbwyList;
		}
		dbwyList.addAll(idList);
		return dbwyList;
	}
	/**
	 * @param time 时间
	 * @param reads 读取次数
	 * @return 获取北斗sqlservice数据
	 **/
	@Override
	@DataSource(DataSourceType.db2)
	@Transactional(readOnly=true)
	public List<TbsDbwy> findDbwyListNoReads(String time,Set<String> devIdList) {
		List<TbsDbwy> dbwyList=new ArrayList<>();
		List<String> ids = new ArrayList<String>();
		for(String id : devIdList) {
			ids.add(id);
		}
		List<TbsDbwy> idList=tbsDbwyMapper.findTbsDbwyListNoReads(time,ids);
		log.info("读取数据为:"+idList);
		if(idList==null || idList.isEmpty()) {
			return dbwyList;
		}
		dbwyList.addAll(idList);
		return dbwyList;
	}
	/**
	 * @param idList
	 * @return 编辑已读
	 **/
	@Override
	@DataSource(DataSourceType.db2)
	@Transactional(rollbackFor=RuntimeException.class)
	public void updateBtachList(List<Integer> idList) {
		log.info("更新id列表为:"+idList);
		tbsDbwyMapper.updateBtachList(idList);
	}
	/**
	 * @param createTime 创建时间
	 * @param deviceNo 设备编号
	 * @return 根据创建时间与设备编号查询6要素列表
	 **/
	@Override
	@Transactional(readOnly=true)
	@DataSource(DataSourceType.db3)
	public List<TbsDbwy6> findDbwy6List(String createTime,String deviceNo) {
		List<TbsDbwy6> list6 = new ArrayList<TbsDbwy6>();
		List<TbsDbwy6> data = tbsDbwy6Mapper.findTbsDbwy6List(createTime,deviceNo);
		if(data==null || data.isEmpty()) {
			return list6;
		}
		return data;
	}
	@Override
	@DataSource(DataSourceType.db1)
	public void insertDbwy(TbsDbwy dbwy) {
		tbsDbwyMapper.insertDbwy(dbwy);
	}
}